﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using JXC.Core.Models;
using JXC.EFCore;

namespace JXC.WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ShouldBeOfVouchersController : ControllerBase
    {
        private readonly AppDbContext _context;

        public ShouldBeOfVouchersController(AppDbContext context)
        {
            _context = context;
        }

        // GET: api/ShouldBeOfVouchers
        [HttpGet]
        public async Task<ActionResult<IEnumerable<ShouldBeOfVoucher>>> GetShouldBeOfVoucher()
        {
            return await _context.ShouldBeOfVoucher.ToListAsync();
        }

        // GET: api/ShouldBeOfVouchers/5
        [HttpGet("{id}")]
        public async Task<ActionResult<ShouldBeOfVoucher>> GetShouldBeOfVoucher(Guid id)
        {
            var shouldBeOfVoucher = await _context.ShouldBeOfVoucher.FindAsync(id);

            if (shouldBeOfVoucher == null)
            {
                return NotFound();
            }

            return shouldBeOfVoucher;
        }

        // PUT: api/ShouldBeOfVouchers/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutShouldBeOfVoucher(Guid id, ShouldBeOfVoucher shouldBeOfVoucher)
        {
            if (id != shouldBeOfVoucher.Id)
            {
                return BadRequest();
            }

            _context.Entry(shouldBeOfVoucher).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ShouldBeOfVoucherExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/ShouldBeOfVouchers
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<ShouldBeOfVoucher>> PostShouldBeOfVoucher(ShouldBeOfVoucher shouldBeOfVoucher)
        {
            _context.ShouldBeOfVoucher.Add(shouldBeOfVoucher);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetShouldBeOfVoucher", new { id = shouldBeOfVoucher.Id }, shouldBeOfVoucher);
        }

        // DELETE: api/ShouldBeOfVouchers/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteShouldBeOfVoucher(Guid id)
        {
            var shouldBeOfVoucher = await _context.ShouldBeOfVoucher.FindAsync(id);
            if (shouldBeOfVoucher == null)
            {
                return NotFound();
            }

            _context.ShouldBeOfVoucher.Remove(shouldBeOfVoucher);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool ShouldBeOfVoucherExists(Guid id)
        {
            return _context.ShouldBeOfVoucher.Any(e => e.Id == id);
        }
    }
}
